# -*- coding: utf-8 -*-
#!usr/bin/python3
# Author: Hermit_Yoshino
'''
@author: Hermit_Yoshino
@license: MIT
@contact: Efreet_Itsukakotori@hotmail.com
@file: index.py
@time: 20-4-27 下午8:43
@desc:
@git: https://gitee.com/rorikontesu/Yanweihua_homework
'''

import pandas as pd
import tensorflow as tf

def csvread(file = './iris_training.csv'):
    tfr_data = pd.read_csv(file, encoding='gbk', engine='python', chunksize=1000
                              , iterator=True)
    return tfr_data.get_chunk(100000)

def main():
    df_training_data = csvread()
    df_training_data.columns = range(df_training_data.shape[1])

    x = df_training_data.iloc[:, 0: 4].values
    y = df_training_data.iloc[:, -1].values
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(6, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
    model.summary()
    model.compile(
        optimizer=tf.keras.optimizers.Adam(),
        # loss=tf.keras.losses.categorical_crossentropy,
        loss=tf.keras.losses.sparse_categorical_crossentropy,
        metrics=['acc']
    )
    model.fit(x, y, epochs=100)
    # model.save('./001.h5')

    # df_test_data = csvread('./iris_test.csv')
    # df_test_data.columns = range(df_test_data.shape[1])
    # lis = model.predict(df_test_data.iloc[:, 0: 4])
    # for i in lis:
    #     i = i.tolist()
    #     print (i.index(max(i)))
    # print (df_test_data.iloc[:, -1])

def test():
    model = tf.keras.models.load_model('./001.h5')
    df_test_data = csvread('./iris_test.csv')
    df_test_data.columns = range(df_test_data.shape[1])
    lis = model.predict(df_test_data.iloc[:, 0: 4].values)
    j = 0
    true = 0
    for i in lis:
        i = i.tolist()
        print ("预测结果：" + str(i.index(max(i)))," 正确结果：" + str(df_test_data.iloc[:, -1][j]))
        if i.index(max(i)) == df_test_data.iloc[:, -1][j]:
            true+=1
        j+=1
    print ("正确率:" + str(int(true*100/len(df_test_data[0]))) + "%")

    # print ()


if __name__ == '__main__':
    # main()
    test()